﻿#include "EquipStrengthDataDescEx.h"

#include <DescStore/EquipDesc.h>
#include <DescStore/EquipStrengthDesc.h>

EquipStrengthDataDescEx::EquipStrengthDataDescEx()
{
	if (EN_OBJ_MODE_INIT == NFShmMgr::Instance()->GetCreateMode()) {
		CreateInit();
	}
	else {
		ResumeInit();
	}
}

EquipStrengthDataDescEx::~EquipStrengthDataDescEx()
{
}

int EquipStrengthDataDescEx::CreateInit()
{
	return 0;
}

int EquipStrengthDataDescEx::ResumeInit()
{
	return 0;
}

int EquipStrengthDataDescEx::Load()
{
	return 0;
}

int EquipStrengthDataDescEx::CheckWhenAllDataLoaded()
{
	m_QualityPartStrengthLevelEquipLevelComIndexMap.clear();
	int count = 0;
	for (int i = 0; i < (int)EquipStrengthDesc::Instance()->GetResNum(); i++)
	{
		const E_EquipStrength* pDesc = EquipStrengthDesc::Instance()->GetDescByIndex(i);
		CHECK_NULL(0, pDesc);
		EquipStrengthQualityPartStrengthLevelEquipLevel data;
		data.quality = pDesc->quality;
		data.part = pDesc->part;
		data.strength_level = pDesc->strength_level;
		for (int level = pDesc->min_equip_level; level <= pDesc->max_equip_level; level++)
		{
			data.equip_level = level;
			count++;
		}
	}

	CHECK_EXPR(count <= (int)m_QualityPartStrengthLevelEquipLevelComIndexMap.max_size(), -1, "count:{} max_size:{}", count, m_QualityPartStrengthLevelEquipLevelComIndexMap.max_size());
	for (int i = 0; i < (int)EquipStrengthDesc::Instance()->GetResNum(); i++)
	{
		const E_EquipStrength* pDesc = EquipStrengthDesc::Instance()->GetDescByIndex(i);
		CHECK_NULL(0, pDesc);
		EquipStrengthQualityPartStrengthLevelEquipLevel data;
		data.quality = pDesc->quality;
		data.part = pDesc->part;
		data.strength_level = pDesc->strength_level;
		for (int level = pDesc->min_equip_level; level <= pDesc->max_equip_level; level++)
		{
			data.equip_level = level;
			CHECK_EXPR(m_QualityPartStrengthLevelEquipLevelComIndexMap.size() <  m_QualityPartStrengthLevelEquipLevelComIndexMap.max_size(), -1, "m_QualityPartStrengthLevelEquipLevelComIndexMap.emplace failed, {}", data.ToString());
			auto result = m_QualityPartStrengthLevelEquipLevelComIndexMap.emplace(data, i);
			CHECK_EXPR(result.first != m_QualityPartStrengthLevelEquipLevelComIndexMap.end() && result.second, -1, "m_QualityPartStrengthLevelEquipLevelComIndexMap.emplace failed, {}", data.ToString());
		}
	}
	return 0;
}

const E_EquipStrength* EquipStrengthDataDescEx::GetDesc(int iStrengthLevel, int iEquipDescID) const
{
	auto pDesc = EquipDesc::Instance()->GetDesc(iEquipDescID);
	if (pDesc)
	{
		EquipStrengthQualityPartStrengthLevelEquipLevel data;
		data.quality = pDesc->quality;
		data.part = pDesc->part;
		data.equip_level = pDesc->dress_level;
		data.strength_level = iStrengthLevel;
		auto iter = m_QualityPartStrengthLevelEquipLevelComIndexMap.find(data);
		if (iter != m_QualityPartStrengthLevelEquipLevelComIndexMap.end())
		{
			return EquipStrengthDesc::Instance()->GetDesc(iter->second);
		}
	}
	return nullptr;
}